package main

import (
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/load"
	"log"
	"net"
	"strings"
)

//获取服务器IP地址
func GetLocalIp() string {
	conn, err := net.Dial("tcp", "192.168.47.135:22")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	addr := conn.LocalAddr().(*net.TCPAddr)
	localAddr := addr
	fmt.Println(localAddr.String())
	return strings.Split(localAddr.IP.String(), ":")[0]
}

// cpu info
func getCpuInfo() {
	cpuInfos, err := cpu.Info()
	if err != nil {
		fmt.Printf("get cpu info failed, err:%v", err)
	}
	for _, ci := range cpuInfos {
		fmt.Println(ci) //cpu信息
	}
	// CPU使用率
	//for {
	//	percent, _ := cpu.Percent(time.Second, false)
	//	fmt.Printf("cpu percent:%v\n", percent)
	//}
}

//cpuload
func getCpuLoad() {
	info, _ := load.Avg()
	fmt.Printf("%v\n",info)
}

// host info
func getHostInfo() {
	hInfo, _ := host.Info()
	fmt.Printf("类型是:%T,host info:%v uptime:%v boottime:%v\n", hInfo, hInfo, hInfo.Uptime, hInfo.BootTime)
}
func main() {
	GetLocalIp()
	getCpuInfo()
	getCpuLoad()
	getHostInfo()
}
